home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 404 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: eerie.acsu.buffalo.edu!newserve!rebecca!rpi!not-for-mail
  2. From: rmartin@oma.com (Robert C. Martin)
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Re: Meaning of the specifier volatile?
  5. Date: 4 Jan 1996 02:49:08 -0000
  6. Organization: Object Mentor
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: herbs@connobj.com
  9. Message-ID: <4cff74$gfj@netlab.cs.rpi.edu>
  10. References: <4c9740$27n@netlab.cs.rpi.edu>
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12.  
  13. X-Original-Date: 03 Jan 1996 19:38:59 GMT
  14.  
  15. In article <4c9740$27n@netlab.cs.rpi.edu> Srinivas Vobilisetti
  16. <srv@cs.wayne.edu> writes:
  17.  
  18.    Nowhere i could find exact meaning of the specifier volatile.
  19.  
  20. Consider the following piece of code:
  21.  
  22. int n;
  23. for (int i=0; i<99; i++)
  24.   n=0;
  25.  
  26. A good optimising compiler should rewrite this as:
  27.  
  28. int n=0;
  29. int i=100;
  30.  
  31. {That's right, if you want to delay for 100 micro seconds, the above
  32. is not a portable approach.}
  33.  
  34. The use of the keyword volatile prevents the compiler from optimising
  35. the reads and writes to a variable.  
  36.  
  37. volatile int n;
  38. for (int i=0; i<99; i++)
  39.   n=0;
  40.  
  41. Will cause zero to be written into the variable n 100 times. 
  42. {Note, the compiler may be smart enough to do this without using the
  43. variable i!}.
  44.  
  45. volatile int n;
  46. for (volatile int i=0; i<99; i++)
  47.   n=0;
  48.  
  49. Will cause zero to be written into the variable n 100 times.  It will
  50. also cause the variable i to be incremented inbetween writes to n, and
  51. will read i to test it for 99 each time through the loop.
  52.  
  53. Generally, this is used when certain memory registers have side
  54. effects.  e.g.
  55.  
  56. int * volatile tty = 0x100;  // 0x100 is the addr of the tty device.
  57.  
  58. *tty = 'h'; // prints a 'c' on the tty;
  59.  
  60. void PrintMessage(char *s)
  61. {
  62.   for(; *s; s++) *tty = *s; // prints the string on the tty.
  63. }
  64.  
  65. You can also use volatile if you want to create stupid delays as in
  66. the first example.
  67.  
  68. --
  69. Robert Martin       | Design Consulting   | Training courses offered:
  70. Object Mentor Assoc.| rmartin@oma.com     |   OOA/D, C++, Advanced OO
  71. 14619 N. Somerset Cr| Tel: (708) 918-1004 |   Mgt. Overview of OOT
  72. Green Oaks IL 60048 | Fax: (708) 918-1023 | Development Contracts.
  73.  
  74.  
  75.     [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
  76.     [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
  77.     [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
  78.     [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
  79.